package com.invengo.module_check;

import android.app.Application;
import android.text.TextUtils;

import androidx.lifecycle.ProcessLifecycleOwner;

import com.invengo.base.base.IModuleInit;
import com.invengo.library_db.GreenDaoManager;
import com.invengo.library_db.utils.DataTestUtils;
import com.invengo.library_rfid.RFIDProcessObserver;
import com.invengo.module_check.data.entity.ActionArgs;
import com.invengo.module_check.data.entity.FeatureBean;

import java.util.ArrayList;

import me.goldze.mvvmhabit.utils.cache.ACache;
import me.goldze.mvvmhabit.utils.cache.AKey;
import me.goldze.mvvmhabit.utils.constant.Constants;

/**
 * Created by goldze on 2018/6/21 0021.
 */

public class CheckModuleInit implements IModuleInit {
    @Override
    public boolean onInitAhead(Application application) {
        initDB(application);
        new Thread(() -> {
            String cacheHost = ACache.getInstance().getAsString(AKey.CACHE_HOST);
            if (TextUtils.isEmpty(cacheHost))
                ACache.getInstance().put(AKey.CACHE_HOST, Constants.DEFAULT_BASE_URL);
        }).start();
        ProcessLifecycleOwner.get().getLifecycle().addObserver(new RFIDProcessObserver(application));
        application.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbackImpl());
        return false;
    }

    private void insertTestData() {
        DataTestUtils.insertCar();
    }

    @Override
    public boolean onInitLow(Application application) {
        new Thread(() -> {
            registerMainFeature();
            insertTestData();
        }).start();
        return false;
    }

    /**
     * 初始化数据库
     */
    private void initDB(Application application) {
        GreenDaoManager.getInstance().setDataBase(application);
    }

    /**
     * 注册首页功能,后期可由文件决定
     */
    private void registerMainFeature() {
        ArrayList<FeatureBean> beans = (ArrayList<FeatureBean>) ACache.getInstance().getAsObject(AKey.CACHE_FEATURE_LIST);
        if (beans != null) return;
        beans = new ArrayList<>();
        beans.add(new FeatureBean(ActionArgs.ACTION_LISTING_GUIDE, FeatureBean.PUBLIC_MODULE, true));
        beans.add(new FeatureBean(ActionArgs.ACTION_PARALLEL, FeatureBean.PUBLIC_MODULE, true));
        beans.add(new FeatureBean(ActionArgs.ACTION_INVENTORY, FeatureBean.PUBLIC_MODULE, true));

        //
        beans.add(new FeatureBean(ActionArgs.ACTION_FIRST, FeatureBean.FIRST_MODULE, false));
        beans.add(new FeatureBean(ActionArgs.ACTION_FIRST_CHECK, FeatureBean.FIRST_MODULE, false));
        beans.add(new FeatureBean(ActionArgs.ACTION_FORCE, FeatureBean.FIRST_MODULE, false));
        beans.add(new FeatureBean(ActionArgs.ACTION_REVOKE, FeatureBean.FIRST_MODULE, false));

        beans.add(new FeatureBean(ActionArgs.ACTION_LISTING, FeatureBean.LAYER_MODULE, true));
        beans.add(new FeatureBean(ActionArgs.ACTION_DOWN, FeatureBean.PUBLIC_MODULE, true));
        beans.add(new FeatureBean(ActionArgs.ACTION_CHANGE, FeatureBean.LAYER_MODULE, true));
        beans.add(new FeatureBean(ActionArgs.ACTION_SEARCH, FeatureBean.PUBLIC_MODULE, true));
        beans.add(new FeatureBean(ActionArgs.ACTION_OFFLINE_COLLECT, FeatureBean.PUBLIC_MODULE, true));
        beans.add(new FeatureBean(ActionArgs.ACTION_REMOVE, FeatureBean.PUBLIC_MODULE, true));


        beans.add(new FeatureBean(ActionArgs.ACTION_FIND_ERROR, FeatureBean.PUBLIC_MODULE, false));
        beans.add(new FeatureBean(ActionArgs.ACTION_GET_IN_CAR, FeatureBean.PUBLIC_MODULE, false));
        beans.add(new FeatureBean(ActionArgs.ACTION_LISTING_BY_CAR, FeatureBean.PUBLIC_MODULE, false));
        ACache.getInstance().put(AKey.CACHE_FEATURE_LIST, beans);
    }
}
